#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpx_supt.c"
#include <dos.h>
#include <dir.h>
#include <time.h>

// for display directories
#define MAX_PATH_SIZE 50
#define ERR_DIR_OPEN  10
#define ERR_NO_FILE 20

#define SUCCESS 0


/* static char array to store the current directory */
static char curr_path[MAX_PATH_SIZE];

/* Entry of ffblk  */
static struct ffblk file_block;

/* */
static int num_entries;
static struct date sys_date;        /* MPX system date */


//Version information
float vers = 1.0;
int modMonth = 9;
int modDay = 19;
int modYear = 2008;


//global variables for readcmd (), processcmd()
static int x=1; //x is our int length of command string, to be used later, y is used to keep track of number of paramaters
static char *scmd = {NULL};
static char *par1, *par2, *par3;  //Parameter 1, 2, 3


// declaration of functions
void readcmd();
//void clearcmd();
void processcmd();
void mpx_exit();
void version();
void disp( );
int help(char func[]);
void welcome();

		
void date(char opt[3], char dte[10]);


int main(int argc, char **argv)
{
	int asdf=0;
	welcome();
	getdate(&sys_date);
	//added: use an endless cycle to read, process commands.
	//added:  match the input commands with our commands libray and
	//added:  decide which functions should be loaded.

	while (1)
	{
		readcmd();
		processcmd();

		if (!strcmp(par1,"help"))
		{
			printf("Load help function.\n");
			if (help(par2))
				printf("help function successfully loaded.\n");
			else
				printf("help function failed.\n");
		}
		else if ( !strcmp(par1,"version"))
		{
			printf("Load version function.\n");
			version();
		}
		else if ( !strcmp(par1,"date"))
		{
			printf("Load date function.\n");
			date(par2,par1);
		}
		else if ( !strcmp(par1,"display") )
		{
			printf("Load display function.\n");
			disp();
		}
		else if ( !strcmp(par1,"exit") )
		{
			printf("Load exit function.\n");
			exit(1);
		}
		else
		{
			printf("Unrecognized command.Try again.\n");
		}
	}

	printf("ASDF");
	scanf("%d",asdf);
//	while(1==1)
//		printf("# %s # %d\n",&par1, x);
}


void welcome ()
{
	printf ("Welcome to MPX\n");
	printf("Please make a valid selection using the command prompt.\n");
	printf(" Enter your selection as either the number corresponding to the comman.\n");
	printf(" the command name itself (with or without paramaters, or any aliases.\n");
	printf("1  Set/Get Date\n");
	printf("2  Display Files\n");
	printf("3  Version\n");
	printf("4  Help\n");
	printf("5  Exit\n");
}


void readcmd()
{
	printf("\n\nMPX>");
//	getchar();
//	scanf("%[ a-zA-Z0-9+*/-]",&scmd); //Scans the command line, including spaces
	gets(&scmd);
	puts(&scmd);
//	x=strlen(&scmd);
}

// The process function takes the string scmd, the command given by the user
// and passes it to the appropriate function with the proper paramaters

void processcmd()
{
	int y=0;
	char *p;

	printf ("Split \"%s\" in tokens:\n", &scmd);

	p = strtok (&scmd," ");

	par1 =p;
	while (p != NULL)
	{
		printf ("%s # %d\n", p, y);
		p = strtok (NULL, " ,");
		if (y==0)
			par2=p;
		else
			par3=p;
		y++;
	}

	printf ("the 1st parameter is %s\n", par1);
	printf ("the 2nd parameter is %s\n", par2);
	printf ("the 3rdt parameter is %s\n", par3);
	printf ("total parameters is %d\n",y);
}


void mpx_exit()
{
	char ex;
	printf("Do you want to exit?\n");
	scanf("%c", &ex);

	printf("the input is %c",ex);
	if(x == 'y' || x == 'Y')
	{
		printf("Now exiting.\n");
		exit(1);
	}
	else if(x == 'n' || x == 'N')
	{
		return;
	}
	else
	{
		printf("Invalid command.\n");
		return;
	}
}


void version()
{
	printf("Version Number: ");
	printf("%f\n",vers);
	printf("Last modified: ");
	printf("%d / %d / %d\n", modMonth, modDay, modYear);
}


void disp( )
{
	char namebuf[60];
	long *filesize;
	/*sys_open_dir ("d:\\tc3\\turboc3\\mpx");   */
	if (par2==NULL)
		sys_open_dir (NULL);
	else
		sys_open_dir (par2);

	sys_get_entry ( namebuf, 60, filesize);
	sys_close_dir();
	return (SUCCESS);
}



/* open a directory, retrun zero if successful, otherwise return non-zero integers to represent various errors. */

int sys_open_dir ( char dir_name[] )
{
	int res;
	char temp_path[MAX_PATH_SIZE];
	char file_type1[]="\\*.*";
	char file_type2[]="*.*";
	/* for conuity, have to think of whether the directory is already opened. */
	if ( curr_path[0]!= '\0') /* The directory is already open */
		return (ERR_DIR_OPEN);

	temp_path[0]='\0';
	/* Analyze the dir_name,which is either NULL, or some path. */

	if (dir_name!=NULL) /* No parameter, just open current directory */
	{
		strcpy(temp_path,dir_name);

		strcat(curr_path, temp_path);
		printf("Current directory is %s\n",temp_path);

		/* check whether there are some files there,otherwise return error code-ERR_NO_FILE  */
		strcat(curr_path,file_type1);
	}
	else
	{
		strcat(curr_path,file_type2);
	}
	printf("Current search files are %s\n",curr_path);
	res = findfirst(curr_path,&file_block,0);
	printf("%d\n",res);

	if (!res)
	{
		num_entries = 0;
		return (SUCCESS);
	}
	/* Set the curr_path*/
	else
	{
		printf("Can't locat files!\n");
		return ( ERR_NO_FILE);
	}
}

/* sys_get_entry: get a directory entry */
/* RETURNS: integer error code, zero if success.*/
int sys_get_entry (char name_buf[], /* file name buffer */
			int  buf_size, /* name buffer size */
			long *file_size_p /* file size variable */
			)
{
	int ent;

	ent = findfirst ( curr_path, &file_block, 0);
	num_entries ++;

	while (!ent)
	{
		strcpy(name_buf, file_block.ff_name);
		*file_size_p = file_block.ff_fsize;
		printf ("%40s\t", name_buf);
		printf ("%10ld\n", *file_size_p);
		ent = findnext (&file_block);
		num_entries++;
	}

	printf ("Totally %d files have been found.", num_entries);
	return (SUCCESS);
}


/* sys_close_dir: close a directory */
/* RETURNS: integer error code, zero if OK */

int sys_close_dir (void)
{
	curr_path[0]='\0';
	return (0);
}


int help(char func[])
{
	FILE *file;
	char s[80], t[80];

	char fn[20]="HELP\\";

	printf("Entering help...\n \n");

	if(func == NULL)
	{
		file=fopen("HELP\\COMMAND.TXT","r");
		if(file==NULL)
		{
			printf("Cannot open command list.  Sorry :( \n \n");
			return 1;
		}
		else
		{
			while(fgets(s, 80, file))
			{
				fputs(s, stdout);
				return 1;
			}
			printf("\n \n");
		}
	}
	else
	{
		strcat(fn,func);
		strcat(fn,".HLP");

		file=fopen(fn,"r");
		if(file==NULL)
		{
			printf("Cannot open specified function definition.  Sorry :( \n \n");
			return 1;
		}
		else
		{
			while(fgets(s, 80, file))
			{
				fputs(s, stdout);
			}
			printf("\n");
			printf("\n");
			return 1;
		}
	}
} //end help


void sys_get_date (date_rec*      date_p)   /* ptr to date record */
{
	date_p->month = sys_date.da_mon;
	date_p->day = sys_date.da_day;
	date_p->year = sys_date.da_year;
}


int sys_set_date (  date_rec*       date_p)   /* ptr to date record */
{
	sys_date.da_mon = date_p->month;
        sys_date.da_day = date_p->day;
        sys_date.da_year = date_p->year;

	return(OK);
}


void date(char opt[3], char dte[10])
{
	char *p, temp;
	int i=0;
	date_rec aDate;

	//test
	printf("%s\n", opt);

	if(opt[3]=="set")
	{
		p=strtok(dte,"-");
		while(p!=NULL)
		{
			temp=atoi(p);
			if(i==0)
			{
				aDate.month=temp;
				puts(p);
			}
			if(i==1)
			{
				aDate.day=temp;
				puts(p);
			}
			if(i==2)
			{
				aDate.year=temp;
				puts(p);
			}
			i++;
			p=strtok(NULL, "-");
		}

		sys_set_date(&aDate);
		printf("Date successfully set! \n");
	}

	else if(opt[3]=="get")
	{
		sys_get_date(&aDate);

		switch (aDate.month)
		{
			case 1:
			printf("January %d %d \n", aDate.day, aDate.year);
			break;
			case 2:
			printf("February %d %d \n", aDate.day, aDate.year);
			break;
			case 3:
			printf("March %d %d \n", aDate.day, aDate.year);
			break;
			case 4:
			printf("April %d %d \n", aDate.day, aDate.year);
			break;
			case 5:
			printf("May %d %d \n", aDate.day, aDate.year);
			break;
			case 6:
			printf("June %d %d \n", aDate.day, aDate.year);
			break;
			case 7:
			printf("July %d %d \n", aDate.day, aDate.year);
			break;
			case 8:
			printf("August %d %d \n", aDate.day, aDate.year);
			break;
			case 9:
			printf("September %d %d \n", aDate.day, aDate.year);
			break;
			case 10:
			printf("October %d %d \n", aDate.day, aDate.year);
			break;
			case 11:
			printf("November %d %d \n", aDate.day, aDate.year);
			break;
			case 12:
			printf("December %d %d \n", aDate.day, aDate.year);
			break;
			default:
			printf("Not an actual month?");
			break;
		}
	}

	else
	{
		printf("Not a recognized command \n");
	}
}

